﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using MapEditor.Components;
using MapEditor.Windows.DataBase;
using MapEditor.Windows.EditorWindows;
using ZeldoEngineObjects.Maps;
using ZeldoEngine;
using ZeldoEngineObjects;

namespace MapEditor.EditorWindows
{
    public partial class MainWindows : Form
    {
        private MainWindowsActions controller;
        
        public MainWindows()
        {
            InitializeComponent();

            this.controller = new MainWindowsActions(this);
        }

        public void ShowPage()
        {
            this.Show();
            this.controller.AfficherCarte(getDrawSurface());
        }

        private void MainWindows_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
        }

        private void toolStripNouvelleCarte_Click(object sender, EventArgs e)
        {
            controller.OuvrirFenetreNouvelleCarte();
        }
        
        private void MainWindows_Load(object sender, EventArgs e)
        {
            this.vScrollBarMap.Enabled = false;
            this.hScrollBarMap.Enabled = false;
            
            var projetName = "Projet Test";

            AppParams.GetIns().InitializeProject(projetName, Path.GetDirectoryName(Application.ExecutablePath));

            this.toolStripButtonCouche1.Checked = true;

            var rootNode = new TreeNode(projetName);
            rootNode.ImageIndex = 0;
            rootNode.SelectedImageIndex = 0;
            this.TreeViewMapList.Nodes.Add(rootNode);
        }

        private void pictureBoxChipset_Click(object sender, EventArgs e)
        {

        }

        private void pictureBoxChipset_Paint(object sender, PaintEventArgs e)
        {
            if (AppParams.GetIns().MapLoaded)
            {
                using (var pen = new Pen(Color.Red, 2f))
                {
                    e.Graphics.DrawRectangle(pen, AppParams.GetIns().Selection);
                }
            }
        }
        
        private void pictureBoxChipset_MouseDown(object sender, MouseEventArgs e)
        {
            this.controller.DebuterSelection(e.X, e.Y);
        }

        private void pictureBoxChipset_MouseUp(object sender, MouseEventArgs e)
        {
            this.controller.TerminerSelection(e.X, e.Y);
        }

        private void pictureBoxChipset_MouseMove(object sender, MouseEventArgs e)
        {
            this.controller.SelectionnerTuile(e.X, e.Y);
        }


        private void TreeViewMapList_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node is TreeNodeMap)
            {
                var treeMap = (TreeNodeMap)e.Node;

                if (treeMap != null)
                {
                    this.controller.ChargerCarte(treeMap);
                }
            }
        }

        private void pictureBoxMap_MouseMove_1(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.controller.DessinerTuile(e.X, e.Y);
            }
            
            this.controller.ChangerMapSelectionPosition(e.X, e.Y, AppParams.GetIns().Selection.Width, AppParams.GetIns().Selection.Height);
        }

        private void pictureBoxMap_MouseLeave(object sender, EventArgs e)
        {
            this.controller.ChangerMapSelectionPosition(-1, -1, 0, 0);
        }

        private void pictureBoxMap_Click(object sender, EventArgs e)
        {
            if (e is MouseEventArgs)
            {
                var evts = (MouseEventArgs)e;

                this.controller.DessinerTuile(evts.X, evts.Y);
            }
        }

        private void pictureBoxMap_MouseDown(object sender, MouseEventArgs e)
        {
            this.controller.DessinerTuile(e.X, e.Y);
        }

        private void toolStripButtonCouche1_Click(object sender, EventArgs e)
        {
            this.toolStripButtonCouche1.Checked = true;
            this.toolStripButtonCouche2.Checked = false;
            this.toolStripButtonCouche3.Checked = false;
            this.toolStripButtonCoucheEvent.Checked = false;

            AppParams.GetIns().CurrentLayer = AppParams.Layer.Layer1;
        }

        private void toolStripButtonCouche2_Click(object sender, EventArgs e)
        {
            this.toolStripButtonCouche1.Checked = false;
            this.toolStripButtonCouche2.Checked = true;
            this.toolStripButtonCouche3.Checked = false;
            this.toolStripButtonCoucheEvent.Checked = false;

            AppParams.GetIns().CurrentLayer = AppParams.Layer.Layer2;
        }

        private void toolStripButtonCouche3_Click(object sender, EventArgs e)
        {

            this.toolStripButtonCouche1.Checked = false;
            this.toolStripButtonCouche2.Checked = false;
            this.toolStripButtonCouche3.Checked = true;
            this.toolStripButtonCoucheEvent.Checked = false;

            AppParams.GetIns().CurrentLayer = AppParams.Layer.Layer3;
        }

        private void toolStripButtonCoucheEvent_Click(object sender, EventArgs e)
        {

            this.toolStripButtonCouche1.Checked = false;
            this.toolStripButtonCouche2.Checked = false;
            this.toolStripButtonCouche3.Checked = false;
            this.toolStripButtonCoucheEvent.Checked = true;

            AppParams.GetIns().CurrentLayer = AppParams.Layer.LayerEvent;
        }

        private void MainWindows_Resize(object sender, EventArgs e)
        {
            // TODO Voir pour le redimensionnement
            //var width = this.panelSurfaceMap.Width;
            //var height = this.panelSurfaceMap.Height;
            //this.pictureBoxMap.Width = width;
            //this.pictureBoxMap.Height = height;
            //this.mapSurface.Resize(width, height);
        }

        private void toolStripButtonLibraries_Click(object sender, EventArgs e)
        {
            var lib = new BaseDeDonnees();
            lib.ShowDialog();
        }

        private void hScrollBarMap_Scroll(object sender, ScrollEventArgs e)
        {
            this.controller.ScrollerCarte(MapEditor.Windows.EditorWindows.MainWindowsActions.ScrollType.HScroll, - e.NewValue);   
        }

        private void vScrollBarMap_Scroll(object sender, ScrollEventArgs e)
        {
            this.controller.ScrollerCarte(MapEditor.Windows.EditorWindows.MainWindowsActions.ScrollType.VScroll, -e.NewValue);  
        }

        public void AjouterMapArbre(TreeNodeMap n)
        {
            if (this.TreeViewMapList.SelectedNode != null)
            {
                this.TreeViewMapList.SelectedNode.Nodes.Add(n);
            }
            else
            {
                this.TreeViewMapList.Nodes.Add(n);
            }

            this.TreeViewMapList.SelectedNode = n;
        }

        public void ChargerTileset(MapModel mapSubmited)
        {
            var images = new List<Bitmap>();
            Bitmap finalChipset = null;

            int width = EngineConsts.CHIPSET_WIDTH;

            var chipset = mapSubmited.Chipset;

            images.Add(new Bitmap(@"Content\Autotiles\" + chipset.AutoTile1 + ".png"));
            images.Add(new Bitmap(@"Content\Autotiles\" + chipset.AutoTile2 + ".png"));
            images.Add(new Bitmap(@"Content\Autotiles\" + chipset.AutoTile3 + ".png"));
            images.Add(new Bitmap(@"Content\Autotiles\" + chipset.AutoTile4 + ".png"));
            images.Add(new Bitmap(@"Content\Autotiles\" + chipset.AutoTile5 + ".png"));
            images.Add(new Bitmap(@"Content\Autotiles\" + chipset.AutoTile6 + ".png"));
            images.Add(new Bitmap(@"Content\Autotiles\" + chipset.AutoTile7 + ".png"));

            var tileset = new Bitmap(@"Content\Chipset\" + chipset.NomFichier + ".png");

            finalChipset = new Bitmap(width, tileset.Height + EngineConsts.TILE_SIZE);

            using (Graphics g = Graphics.FromImage(finalChipset))
            {
                g.Clear(Color.Black);

                int offset = EngineConsts.TILE_SIZE;
                foreach (var image in images)
                {
                    g.DrawImage(image, new Rectangle(offset, 0, EngineConsts.TILE_SIZE, EngineConsts.TILE_SIZE), new Rectangle(0, 0, EngineConsts.TILE_SIZE, EngineConsts.TILE_SIZE), GraphicsUnit.Pixel);
                    offset += EngineConsts.TILE_SIZE;
                }

                g.DrawImage(tileset, new Rectangle(0, EngineConsts.TILE_SIZE, tileset.Width, tileset.Height));
            }

            this.pictureBoxChipset.Image = finalChipset;
        }

        public void ChargerAffichageCarte(MapModel mapSubmited)
        {
            // Réglage des scrollbars en fonction de taille de la map
            if (mapSubmited.Width * EngineConsts.TILE_SIZE > this.pictureBoxMap.Width)
            {
                this.hScrollBarMap.Enabled = true;
                this.hScrollBarMap.Maximum = mapSubmited.Width - (this.pictureBoxMap.Width / EngineConsts.TILE_SIZE);
            }
            else
            {
                this.hScrollBarMap.Enabled = false;
            }

            if (mapSubmited.Height * EngineConsts.TILE_SIZE > this.pictureBoxMap.Height)
            {
                this.vScrollBarMap.Enabled = true;
                this.vScrollBarMap.Maximum = mapSubmited.Height - (this.pictureBoxMap.Height / EngineConsts.TILE_SIZE);
            }
            else
            {
                this.vScrollBarMap.Enabled = false;
            }
        }

        public void ActualiserTileset()
        {
            this.pictureBoxChipset.Invalidate();
        }
    }
}
